package com.ma.crawl_book.controller;

import com.ma.crawl_book.model.BookBean;
import com.ma.crawl_book.model.BookBeanCustom;
import com.ma.crawl_book.utils.DateUtil;
import com.ma.crawl_book.utils.DownloadImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

@Component
public class CloneBook implements PageProcessor {


    private Site site = Site.me().setRetryTimes(5).setSleepTime(500);
    public static BookBeanCustom book = new BookBeanCustom();

    @Autowired
    private BookController bookController;

    @Autowired
    private CrawlSection crawlSection;

    @Override
    public Site getSite() {
        // TODO Auto-generated method stub
        return site;
    }

    @Override
    public void process(Page page) {
        // 一级页面（书籍列表）
        // if
        // (page.getUrl().regex("http://www.23us.so/top/allvisit_[0-9]*.html").match())
        // {
        // // 当前页所有的url地址
        // List<String> list =
        // page.getHtml().xpath("//td[@class=\"L\"][1]/a").links().all();
        // // 当前页所有的书名
        // List<String> titleNames =
        // page.getHtml().xpath("//td[@class=\"L\"][1]/a/text()").all();
        // // 添加二级页面的URL地址 （ http://www.23us.so/xiaoshuo/14056.html 重生之财源滚滚）
        // page.addTargetRequests(list);
        // }
        // 二级页面 （书籍详情页）
        if (page.getUrl().regex("http://www.23us.so/xiaoshuo/[0-9]*.html").match()) {
            // 章节页面地址 http://www.23us.so/files/article/html/0/81/index.html
            String sectionUrl = page.getHtml().xpath("//div[@class=\"fl\"][1]/a[@class='hst']/@href").get();
            // 书名
            String bookName = page.getHtml().xpath("//*[@id='content']/dd[1]/h1/text()").replace("全文阅读", "").get()
                    .trim();
            // 书籍封面地址
            String photoPath = page.getHtml().xpath("//div[@class=\"fl\"][1]/a[@class='hst']/img/@src").get();
            // 书籍所属类别
            String bookType = page.getHtml().xpath("//*[@id='at']/tbody/tr[1]/td[1]/a/text()").get();
            // 书籍作者
            String bookAuthor = page.getHtml().xpath("//*[@id='at']/tbody/tr[1]/td[2]/text()").get();
            // 书籍状态
            String bookStatus = page.getHtml().xpath("//*[@id='at']/tbody/tr[1]/td[3]/text()").get();
            // 书籍收藏次数
            String bookCollection = page.getHtml().xpath("//*[@id='at']/tbody/tr[2]/td[1]/text()").get().replaceAll(" ", "");
            // 书籍字数
            String bookWordNumber = page.getHtml().xpath("//*[@id='at']/tbody/tr[2]/td[2]/text()").replace("字", "").replace(" ", "")
                    .get().trim();
            // 最后更新时间
            String bookUpdate = DateUtil.date2TimeStamp(page.getHtml().xpath("//*[@id='at']/tbody/tr[2]/td[3]/text()").get().replace(" ", ""), "yyyy-MM-dd HH:mm:ss");

            // 总点击数
            String clickNumber = page.getHtml().xpath("//*[@id='at']/tbody/tr[3]/td[1]/text()").get().replace(" ", "").trim();
            // 书籍简介
            String content = page.getHtml().xpath("//*[@id='content']/dd[4]/p[2]/text()").get();
            BookBean bookBean = new BookBean();
            bookBean.setBookName(bookName);
            switch (bookType){
                case "玄幻魔法":
                    bookBean.setBookType(6);
                    break;
                case "武侠修真":
                    bookBean.setBookType(7);
                    break;
                case "都市言情":
                    bookBean.setBookType(1);
                    break;
                case "历史军事":
                    bookBean.setBookType(2);
                    break;
                case "网游竞技":
                    bookBean.setBookType(3);
                    break;
                case "科幻小说":
                    bookBean.setBookType(4);
                    break;
                case "恐怖灵异":
                    bookBean.setBookType(5);
                    break;
                case "女生小说":
                    bookBean.setBookType(8);
                    break;
                case "其他小说":
                    bookBean.setBookType(9);
                    break;
                case "全本小说":
                    bookBean.setBookType(10);
                    break;
                    default:
                        break;
            }
            String photo = photoPath.substring(photoPath.lastIndexOf("/") + 1, photoPath.length());
            boolean download = DownloadImage.download(photoPath, photo, "D:\\crawl_image\\");
            if (download) {
                bookBean.setImagePath("/upload/imagePath/book/" + photo);
            } else {
                bookBean.setImagePath(photoPath);
            }
            bookBean.setAuthor(bookAuthor);
            bookBean.setIsEnd(bookStatus.contains("连载中") ? 1 : 0);
            bookBean.setCollection(Integer.parseInt(bookCollection));
            bookBean.setWordNumber(Integer.parseInt(bookWordNumber));
            bookBean.setDate(bookUpdate);
            bookBean.setSex(bookType.equals("女生小说") ? 1 : 2);
            bookBean.setClickNumber(Integer.parseInt(clickNumber));
            bookBean.setContent(content);
            book.setBookSectionUrl(sectionUrl);
            book.setBookBean(bookBean);
        }
    }

    public void startCrawl(String url) {
        System.out.println("【爬虫开始】请耐心等待...");
        Spider.create(new CloneBook()).addUrl(url).thread(30).run();
        System.out.println(book.toString());
        //插入书籍详情信息，并返回书籍id
        int bookId = bookController.insertBook(book.getBookBean());
        crawlSection.startCrawl(bookId, book.getBookSectionUrl());
        System.out.println("【爬虫结束】" + bookId);
    }
}
